package org.opsli.modulars.business.appValveWell.web;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.opsli.modulars.business.app.web.MapCordinatesVo;

import java.math.BigDecimal;

import static java.lang.Math.*;

@Slf4j
public class CoordinatesConvertUtil {

    private  static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    /**
     * 高德地图坐标转化为百度坐标
     */
    public static org.opsli.modulars.business.app.web.MapCordinatesVo bd_encrypt(org.opsli.modulars.business.app.web.MapCordinatesVo cordinatesVo)

    {
        if(cordinatesVo.getLat() == null || cordinatesVo.getLon() == null) {
            log.info("地图坐标不能为空，{}", JSON.toJSONString(cordinatesVo));
            return cordinatesVo;
        }

        double x = cordinatesVo.getLon().doubleValue(), y = cordinatesVo.getLat().doubleValue();

        double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);

        double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);

        double bd_lon = z * cos(theta) + 0.0065;

        double bd_lat = z * sin(theta) + 0.006;

        org.opsli.modulars.business.app.web.MapCordinatesVo mapCordinatesVo = new org.opsli.modulars.business.app.web.MapCordinatesVo();
        BigDecimal lat = new BigDecimal(bd_lat);
        BigDecimal lon = new BigDecimal(bd_lon);

        mapCordinatesVo.setLat(lat.setScale(6,   BigDecimal.ROUND_HALF_DOWN));
        mapCordinatesVo.setLon(lon.setScale(6,   BigDecimal.ROUND_HALF_DOWN));


        return mapCordinatesVo;
    }




     //百度坐标系转高德
      public static org.opsli.modulars.business.app.web.MapCordinatesVo gd_decrypt(org.opsli.modulars.business.app.web.MapCordinatesVo baiduMapVo)
        {
            double bd_lat=baiduMapVo.getLat().doubleValue();
            double bd_lon=baiduMapVo.getLon().doubleValue();
            double x = bd_lon - 0.0065, y = bd_lat - 0.006;
            double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
            double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
            double gg_lon = z * cos(theta);
            double gg_lat = z * sin(theta);

            BigDecimal lat = new BigDecimal(gg_lat);
            BigDecimal lon = new BigDecimal(gg_lon);
      /*  System.out.println(bd_lat+","+bd_lon);
        System.out.println(gg_lat+","+gg_lon);
        System.out.println(lat.setScale(6,   BigDecimal.ROUND_HALF_DOWN)+","+lon.setScale(6,
        BigDecimal.ROUND_HALF_DOWN));*/
           baiduMapVo.setLat(lat.setScale(6,   BigDecimal.ROUND_HALF_DOWN));
           baiduMapVo.setLon(lon.setScale(6,   BigDecimal.ROUND_HALF_DOWN));
           return baiduMapVo;

        }




        public static void main(String[] args){
           /* MapCordinatesVo cordinatesVo=new MapCordinatesVo();
            cordinatesVo.setLon(new BigDecimal("113.319319"));
            cordinatesVo.setLat(new BigDecimal("34.81192"));
            cordinatesVo=bd_encrypt(cordinatesVo);
            System.out.println(JSON.toJSONString(cordinatesVo));*/
            org.opsli.modulars.business.app.web.MapCordinatesVo cordinatesVo=new MapCordinatesVo();
            cordinatesVo.setLon(new BigDecimal("113.321719"));
            cordinatesVo.setLat(new BigDecimal("34.806124"));

           gd_decrypt(cordinatesVo);
           System.out.println(JSON.toJSONString(cordinatesVo));
        }
}
